package com.heima.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.mapper.WorkMapper;
import com.heima.pojos.User;
import com.heima.pojos.UserStudentAssignment;
import com.heima.pojos.dto.WorkDto;
import com.heima.pojos.dto.findAssignmentsDto;
import com.heima.pojos.vo.showWorksVo;
import com.heima.service.UserService;
import com.heima.service.WorkService;
import com.heima.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class WorkServiceImpl extends ServiceImpl<WorkMapper, UserStudentAssignment> implements WorkService {

    @Autowired
    private UserService userService;

    /**
     * 作业分页查询
     *
     * @param findAssignments@return
     */
    @Override
    public R selectWork(findAssignmentsDto findAssignments) {
        //SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        //simpleDateFormat  解析前端传来的时间，格式化成数据库存储的时间格式  假设解析完的数据是AAA


        //返回给前端的data数据
        List<showWorksVo> showWorksVos = new ArrayList<>();


        List<User> Users = userService.list();

        //学生个人作业信息
        showWorksVo showWorksVo = new showWorksVo();
        for (User user : Users) {
            List<UserStudentAssignment> list = list(Wrappers.lambdaQuery(UserStudentAssignment.class)
                    .eq(UserStudentAssignment::getUserId, user.getId())
                    //.eq(UserStudentAssignment::getUserId, user.getId())     AAA这里eq一下数据库时间
                    .eq(UserStudentAssignment::getAssignmentType, findAssignments.getType()));

            for (UserStudentAssignment userStudentAssignment : list) {

                //每个学生可能不止一样作业
                WorkDto workDto = new WorkDto();

                workDto.setWorkname(userStudentAssignment.getAssignmentName());
                workDto.setId(userStudentAssignment.getId());
                workDto.setIsComplete(findAssignments.getType());
                showWorksVo.setName(user.getName());
                showWorksVo.setUid(user.getId());
                //todo
//                showWorksVo.setPracticeList(workDto);

                showWorksVos.add(showWorksVo);
            }
        }

        //没写完。。层级嵌套有问题
        return R.OK(showWorksVos);
    }


}
